package jp.co.toshiba.ffsa.libs.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import jp.co.toshiba.ffsa.dto.LoginDto;

import org.seasar.framework.container.hotdeploy.HotdeployHttpSession;
import org.seasar.framework.container.hotdeploy.HotdeployUtil;

/**
 * セッション管理を行います。
 * @author  NPC
 */
public class FFSAExecuteFilter implements Filter {

    private final String[] exclusionDisplayID = {"cstp101","cstp201","csas101","csas102","csas103","csas104","csas105","cser101"};
    /**
    * 起動時は何もしない。
    */
    public void init( FilterConfig arg0 ) throws ServletException {
    }

    /**
    * ログイン状態にあるかを判断します。
    */
    public void doFilter( ServletRequest request, ServletResponse response, FilterChain chain ) throws IOException, ServletException {
        try{
            HttpSession session = ((HttpServletRequest)request).getSession();

            boolean checkFlag = true;
            for(int i=0; i<this.exclusionDisplayID.length; i++){
                if(((HttpServletRequest)request).getServletPath().indexOf(this.exclusionDisplayID[i]) != -1){
                    checkFlag = false;
                }
            }
            if(checkFlag){
                if (session == null){
                    /* まだ認証されていない */
                    ((HttpServletRequest)request).setAttribute("login_flag", false);
                }else{
                    if(session.isNew()){
                        /* まだ認証されていない */
                        ((HttpServletRequest)request).setAttribute("login_flag", false);
                    }else{
                        LoginDto loginDto   = null;
                        Object objectLoginDto   = session.getAttribute("login_dto");
                        if(objectLoginDto != null){
                            if (HotdeployUtil.isHotdeploy()) {
                                //// HOT deployの時 ////
                                HotdeployHttpSession.SerializedObjectHolder serializeSessionDto = (HotdeployHttpSession.SerializedObjectHolder)objectLoginDto;
                                loginDto = (LoginDto)serializeSessionDto.getDeserializedObject("loginDto");
                            } else {
                                //// COOL deployの時 ////
                                loginDto = (LoginDto)objectLoginDto;
                            }
                        }

                        if(loginDto != null){}else{
                            ((HttpServletRequest)request).setAttribute("login_flag", false);
                        }
                    }
                }
            }

            chain.doFilter(request, response);
        }catch (ServletException se){
            throw se;
        }catch (IOException e){
            throw e;
        }
    }

    /**
    * 終了時は何もしない。
    */
    public void destroy(){
    }
}
